package com.ashleyleung.jetpack_b_station.musicList

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Lifecycle.Event.*
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import com.ashleyleung.jetpack_b_station.lifecycle.AbsLifecycle
import com.ashleyleung.jetpack_b_station.lifecycle.ILifeCycleOwner
import com.ashleyleung.jetpack_b_station.lifecycle.LifeState
import com.ashleyleung.jetpack_b_station.lifecycle.ViewLifeImpl
import com.ashleyleung.jetpack_b_station.player.DataListenContainer
import com.ashleyleung.jetpack_b_station.player.domain.Music
import timber.log.Timber

/**
 * @author lxy
 * @name
 * @class describe
 * @date 2022-06-22
 *
 */
class MusicPresenter(owner: LifecycleOwner) {
    // 不写: ViewLifeImpl() 的话就写注释了的内容
    private val viewLifeImpl by lazy {
        ViewLifeImpl()
    }

    //定义内部类的话就可以管理自己的lifecycle
    inner class ViewLifeImpl : LifecycleEventObserver {

        /**
         * @name ViewLifeImpl
         * @describe 被动通知view层的生命周期变化    继承自LifecycleEventObserver，可获取状态作判断。对应DataListenContainer里面用注解方式，
         * @author liangxy
         * @time 2022/6/24 14:35
         * Copyright (c)  Guangzhou GCI Science & Technology Co., Ltd.All rights reserved
         */
        override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
            when (event) {
                ON_CREATE -> {}
                ON_START -> {
                    Timber.e("监听GPS信号变化")
                }
                ON_RESUME -> {}
                ON_PAUSE -> {}
                ON_STOP -> {
                    Timber.e("停止GPS信号变化监听")
                }
                ON_DESTROY -> {}
                ON_ANY -> {}
            }

        }


    }


    init {
        owner.lifecycle.addObserver(viewLifeImpl)


//        owner.getLifecycleProvider().addLifeListener(ViewLifeImpl())
//        owner.getLifecycleProvider().addLifeListener(ViewLifeImpl2())
    }

    enum class MusicLoadState {
        LOADING, EMPTY, SUCCESS, ERROR

    }

    private val musicModel by lazy { MusicModel() }

    val liveMusicList = MutableLiveData<List<Music>>()
    val musicList = DataListenContainer<List<Music>>()

    val loadState = DataListenContainer<MusicLoadState>()

    private val page = 1
    private val size = 20
    fun getMusic() {
        loadState.value = MusicLoadState.LOADING


//从modeL层中获取音乐列表
        musicModel.loadMusicByPage(page, size, object : MusicModel.onMusicLoadResult {
            override fun onSuccess(result: List<Music>) {
//                liveMusicList.value = result  这种不确保主线程更新
                liveMusicList.postValue(result)
             /*
                自己实现的观察者
                musicList.value = result*/
                loadState.value = if (result.isEmpty())
                    MusicLoadState.EMPTY
                else
                    MusicLoadState.SUCCESS
            }

            override fun onError(msg: String, code: Int) {
                loadState.value = MusicLoadState.ERROR
                Timber.e("error...$msg...$code")
            }

        })
    }


}/*class MusicPresenter(owner: ILifeCycleOwner) {
    // 不写: ViewLifeImpl() 的话就写注释了的内容
//    private val viewLifeImpl by lazy {
//        ViewLifeImpl()
//    }
//定义内部类的话就可以管理自己的lifecycle
    inner class ViewLifeImpl2 : AbsLifecycle() {

        override fun onViewLifeStateChange(state: LifeState) {
            Timber.e("onViewLifeStateChange ViewLifeImpl2")
        }

        override fun onCreateLc() {

            //监听GPS信号变化
            Timber.e("监听GPS信号变化")
        }

        override fun onStartLc() {
            //监听GPS信号变化
            Timber.e("监听GPS信号变化")

        }

        override fun onResumeLc() {

        }

        override fun onPauseLc() {


        }

        override fun onStopLc() {
            //停止GPS信号变化监听，
            Timber.e("停止GPS信号变化监听")
        }

        override fun onDestroyLc() {

        }
    }


    init {
        owner.getLifecycleProvider().addLifeListener(ViewLifeImpl())
//        owner.getLifecycleProvider().addLifeListener(ViewLifeImpl2())
    }

    enum class MusicLoadState {
        LOADING, EMPTY, SUCCESS, ERROR

    }

    private val musicModel by lazy { MusicModel() }

    val musicList = DataListenContainer<List<Music>>()

    val loadState = DataListenContainer<MusicLoadState>()

    private val page = 1
    private val size = 20
    fun getMusic() {
        loadState.value = MusicLoadState.LOADING


//从modeL层中获取音乐列表
        musicModel.loadMusicByPage(page, size, object : MusicModel.onMusicLoadResult {
            override fun onSuccess(result: List<Music>) {
                musicList.value = result
                loadState.value = if (result.isEmpty())
                    MusicLoadState.EMPTY
                else
                    MusicLoadState.SUCCESS
            }

            override fun onError(msg: String, code: Int) {
                loadState.value = MusicLoadState.ERROR
                Timber.e("error...$msg...$code")
            }

        })
    }


}*/